"""
Google Maps爬虫 - Scrapy版本
"""
import scrapy
import json
from urllib.parse import urlencode
from typing import List, Dict, Any, Optional
from .base_spider import BaseScrapySpider, SpiderFactory

class GoogleMapsSpider(BaseScrapySpider):
	name = 'google_maps'

	def __init__(self, name=None, search_queries: Optional[List[str]] = None, status_callback=None, api_key: Optional[str] = None, **kwargs):
		super().__init__(name or self.name, query=None, status_callback=status_callback, **kwargs)
		self.search_queries = search_queries or []
		self.api_key = api_key
		self.base_url = "https://maps.googleapis.com/maps/api/place"
		self.custom_settings = {
			'DOWNLOAD_DELAY': 2,
			'CONCURRENT_REQUESTS': 1,
			'CONCURRENT_REQUESTS_PER_DOMAIN': 1,
		}

	def _get_start_requests(self):
		if not self.search_queries or not self.api_key:
			return []
		for q in self.search_queries:
			url = f"{self.base_url}/textsearch/json"
			params = {'query': q, 'key': self.api_key, 'language': 'zh-CN'}
			yield scrapy.Request(
				url=f"{url}?{urlencode(params)}",
				callback=self.parse_places,
				meta={'search_query': q}
			)

	def parse_places(self, response):
		try:
			data = json.loads(response.text)
			for place in data.get('results', []):
				place_id = place.get('place_id')
				if not place_id:
					continue
				url = f"{self.base_url}/details/json"
				params = {
					'place_id': place_id,
					'key': self.api_key,
					'fields': 'name,formatted_address,formatted_phone_number,website,opening_hours'
				}
				yield scrapy.Request(
					url=f"{url}?{urlencode(params)}",
					callback=self.parse_place_details,
					meta={'place': place, 'search_query': response.meta.get('search_query', '')}
				)
		except json.JSONDecodeError:
			self.logger.error("解析Google Maps响应失败")

	def parse_place_details(self, response):
		try:
			data = json.loads(response.text)
			place_details = data.get('result', {})
			place = response.meta.get('place', {})
			yield {
				"search_query": response.meta.get('search_query', ''),
				"company_name": place_details.get('name', place.get('name', '')),
				"contact_name": "",
				"email": "",
				"phone": place_details.get('formatted_phone_number', ''),
				"title": "",
				"address": place_details.get('formatted_address', ''),
				"website": place_details.get('website', ''),
				"source": "google_maps",
				"confidence": 0.9,
				"raw_data": {"place": place, "details": place_details}
			}
		except json.JSONDecodeError:
			self.logger.error("解析地点详情失败")

	def is_free_source(self) -> bool:
		return False

	def get_rate_limit(self) -> float:
		return 2.0

	def get_cost_info(self) -> Dict[str, Any]:
		return {
			"is_free": False,
			"rate_limit": self.get_rate_limit(),
			"description": "Google Maps API - 按使用量计费",
			"api_key_required": True
		}

SpiderFactory.register("google_maps", GoogleMapsSpider)